package com.swordget.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONArray;
import com.swordget.commons.JdbcUtil;
import com.swordget.dao.DataHandlerDao;
import com.swordget.entity.ColumnDesign;
import com.swordget.entity.po.DataQueryPo;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.List;

/**
 * @Author: David
 * @Date: Created in 18:11 2020/4/8
 * @Description: 数据查询服务 dao 层 sqlserver实现
 * @Modified By:
 * @Version: 0.0.1
 */
public class DataHandlerSqlServerDaoImp extends DataHandlerOracleDaoImp implements DataHandlerDao {

    @Autowired
    private DruidDataSource dataSource;

    @Autowired
    private JdbcUtil jdbcUtil;


    @Override
    public DataQueryPo query(int rows, int page, JSONArray params, String tableName, List<String> columns,
                             List<String> types, List<ColumnDesign> columnDesigns, String dataSourceId) {
        //查询数据
        final String queryColumn = String.join(",", columns);
        final String paramSql = handlerParams(params);
        String querySql = String.format("SELECT TOP %d %s FROM %s WHERE %s and (SERIALNO NOT IN " +
                        "(SELECT TOP %d SERIALNO FROM %s WHERE %s ORDER BY CREATEDATE DESC, CREATETIME DESC))" +
                        " ORDER BY CREATEDATE DESC, CREATETIME DESC", rows, queryColumn, tableName,
                paramSql, (page - 1) * rows, tableName, paramSql);
        String countSql = String.format("SELECT COUNT(*) FROM %s WHERE %s", tableName, paramSql);
        return DataQueryPo.builder()
                .data(this.executeQuery(querySql, columns, types, columnDesigns, ""))
                .total(jdbcUtil.executeCount(countSql))
                .build();
    }


    @Override
    public List<List<String>> queryByList(int rows, int page, JSONArray queryParam, String tableName, List<String> columns, List<String> types) {
        final String queryColumn = String.join(",", columns);
        final String paramSql = handlerParams(queryParam);
        String querySql = String.format("SELECT TOP %d %s FROM %s WHERE %s and (SERIALNO NOT IN " +
                        "(SELECT TOP %d SERIALNO FROM %s WHERE %s ORDER BY CREATEDATE DESC, CREATETIME DESC))" +
                        " ORDER BY CREATEDATE DESC, CREATETIME DESC", rows, queryColumn, tableName,
                paramSql, (page - 1) * rows, tableName, paramSql);
        return this.executeQueryByList(querySql, columns, types, null,"");

    }

    @Override
    public List<List<String>> queryByListByStatus(int rows, int page, JSONArray queryParam, String tableName,
                                                  List<String> columns, List<String> types, List<ColumnDesign> columnDesigns,
                                                  String[] serials, String dataSourceId) {
        //查询数据
        final String queryColumn = String.join(",", columns);
        final String paramSql = this.handlerParams(queryParam, serials);
        String querySql = String.format("SELECT TOP %d %s FROM %s WHERE %s and (SERIALNO NOT IN " +
                        "(SELECT TOP %d SERIALNO FROM %s WHERE %s ORDER BY CREATEDATE DESC, CREATETIME DESC))" +
                        " ORDER BY CREATEDATE DESC, CREATETIME DESC", rows, queryColumn, tableName,
                paramSql, (page - 1) * rows, tableName, paramSql);
        return this.executeQueryByList(querySql, columns, types, null,"");
    }

    @Override
    public int insert(File loadData, String tableName, List<String> columns) throws Exception {


        return super.insert(loadData, tableName, columns);
    }

}
